# -*- encoding: utf-8 -*-

from Common.mysql import db
from Common.public_method import utc_8
from Common.yaml_method import YamlMethod
from src.ApiTest.Operating.Database.operating_database import OperatingConfig


class AddOperatingConfig:
    """
    添加运行配置方法
    """

    @staticmethod
    def add_operating_config(config_name, project_id, project_name, environment, create_user):
        """
        添加运行配置
        :param config_name: 配置名称
        :param project_id: 项目ID
        :param project_name: 项目名称
        :param environment: 运行环境
        :param create_user: 创建人
        :return:
        """

        code = YamlMethod().read_data('code.yaml')['code']

        info = OperatingConfig.query.filter_by(configName=config_name, projectId=project_id).first()
        if info is None:
            create_time = utc_8()
            # 项目信息插入数据库
            info = OperatingConfig(configName=config_name, projectId=project_id, projectName=project_name,
                                   environment=environment, create_time=create_time, create_user=create_user)
            db.session.add(info)
            db.session.commit()

            res = {
                'code': code[0],
                'message': 'success',
                'data': {}
            }
            return res
        else:
            res = {
                'code': code[1],
                'message': '配置名称已存在',
                'data': {}
            }
            return res

